//
//  wireless_os.c
//  wireless_sensor
//
//  Created by fukai on 2018/11.
//  Copyright © 2018年 赛博智能车实验室. All rights reserved.
//

#include "oled.h"
#include "led.h"
#include "beep.h"
#include "wireless_os.h"

/********主机部分********/

//数据发送缓冲区
unsigned char MasterDataBuffer[33] = {0};

WiSenMasterNode WiSenMaster =
{
	.temperature = 0,	//温度
	.humidity = 0,		//湿度
	.data_buffer = MasterDataBuffer,
	.DataReceiveStatus = RECEIVE_FAILD,
	.MasterAlertLevel = MASTER_NORMAL,
};

//传感器数据缓冲区更新
void MasterDataBufferUpdate(void)
{
	WiSenMaster.temperature = *WiSenMaster.data_buffer;	//第一个数据是温度
	WiSenMaster.humidity = *(WiSenMaster.data_buffer+1);
}

//主机接收信息分析
void MasterDataAnalysis(void)
{
	//传感器报警状态分析
	if(WiSenMaster.temperature<=40 && WiSenMaster.humidity<=80)
	{
		WiSenMaster.MasterAlertLevel = MASTER_NORMAL;
	}
	else if(WiSenMaster.temperature>45 || WiSenMaster.humidity>90)
	{
		WiSenMaster.MasterAlertLevel = MASTER_RED_ALERT;
	}
	else
	{
		WiSenMaster.MasterAlertLevel = MASTER_YELLOW_ALERT;
	}
}

//主机报警处理
//用于中断中
void MasterAlertProcess(void)
{
	switch(WiSenMaster.MasterAlertLevel)
	{
		case MASTER_NORMAL:
			LedOff();
			BeepStop();
			break;
		case MASTER_YELLOW_ALERT:
			LedOn();
			BeepStop();
			break;
		case MASTER_RED_ALERT:
			FlowLampOn(100);
			BeepSing(100);
			break;
	}
}

//主机用户界面显示
void MasterUserInterface(void)
{
	OledShow6x8Str(10,2,"TEMP:");	//当前温度
	OledDoubleValueShow(50,2,WiSenMaster.temperature);
	OledShow6x8Str(65,2,"C");
	OledShow6x8Str(10,4,"HUMI:");	//当前湿度
	OledDoubleValueShow(50,4,WiSenMaster.humidity);
	OledShow6x8Str(65,4,"RH");
	
	OledShow6x8Str(10,6,"------------------");
	switch(WiSenMaster.DataReceiveStatus)
	{
		case RECEIVE_OK:
			OledShow6x8Str(15,7,"--RECEIVE OK!!--");
			break;
		case RECEIVE_FAILD:
			OledShow6x8Str(15,7,"-RECEIVE FAILD!-");
			break;
	}
}

/********从机部分********/

//数据发送缓冲区
unsigned char SlaveDataBuffer[33] = {0};

WiSenSlaveNode WiSenSlave = 
{
	.temperature = 0,	//温度
	.humidity = 0,		//湿度
	.data_buffer = SlaveDataBuffer,
	
	.DataSendStatus = SEND_FAILD,//数据传输状态
	.AlertLevel = NORMAL,//传感器报警状态
	
};

//传感器信息分析
void DataAnalysis(void)
{
	//传感器报警状态分析
	if(WiSenSlave.temperature<=40 && WiSenSlave.humidity<=80)
	{
		WiSenSlave.AlertLevel = NORMAL;
	}
	else if(WiSenSlave.temperature>45 || WiSenSlave.humidity>90)
	{
		WiSenSlave.AlertLevel = RED_ALERT;
	}
	else
	{
		WiSenSlave.AlertLevel = YELLOW_ALERT;
	}
}

//传感器报警处理
//用于中断中
void SensorAlertProcess(void)
{
	switch(WiSenSlave.AlertLevel)
	{
		case NORMAL:
			LedOff();
			BeepStop();
			break;
		case YELLOW_ALERT:
			LedOn();
			BeepStop();
			break;
		case RED_ALERT:
			FlowLampOn(100);
			BeepSing(100);
			break;
	}
}

//传感器数据缓冲区更新
void DataBufferUpdate(void)
{
	*WiSenSlave.data_buffer = WiSenSlave.temperature;	//第一个数据是温度
	*(WiSenSlave.data_buffer+1) = WiSenSlave.humidity;
}

//用户界面显示
void user_interface(void)
{
	OledShow6x8Str(10,2,"TEMP:");	//当前温度
	OledDoubleValueShow(50,2,WiSenSlave.temperature);
	OledShow6x8Str(65,2,"C");
	OledShow6x8Str(10,4,"HUMI:");	//当前湿度
	OledDoubleValueShow(50,4,WiSenSlave.humidity);
	OledShow6x8Str(65,4,"RH");
	
	OledShow6x8Str(10,6,"------------------");
	
	switch(WiSenSlave.DataSendStatus)
	{
		case SEND_OK:
			OledShow6x8Str(20,7,"---SEND OK!!---");
			break;
		case SEND_FAILD:
			OledShow6x8Str(20,7,"--SEND FAILD!--");
			break;
	}
}






